<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty overtime modifier plugin
 * author: allan@tudou.com/zeus@tudou.com 
 * date:   20070112
 * Type:   modifier<br>
 * Name:   iconv<br>
 * Purpose: 将某时间和现在对比，输出类似：3分钟前
 */

function smarty_modifier_overtime($sTime)
{
	static $sNowStamp;
	if (!isset($sNowStamp)) {
		$sNowStamp = time();
	}

	$nStamp = strtotime($sTime);
	if (!(0 < $nStamp)) {
		return '某时';
	}

	if ($nStamp > $sNowStamp) {
		$sFix = '后';
		$nSecond = $nStamp - $sNowStamp;
	}
	else {
		$sFix = '前';
		$nSecond = $sNowStamp - $nStamp;
	}
	if (86400 < $nSecond) {
		return floor($nSecond/86400).'天'.$sFix;
	}
	elseif (3600 < $nSecond) {
		return floor($nSecond/3600).'小时'.$sFix;
	}
	elseif (60 < $nSecond) {
		return floor($nSecond/60).'分钟'.$sFix;
	}
	else {
		return floor($nSecond).'秒'.$sFix;
	}
}

?>
